/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.inputQuestion;

import javax.inject.Inject;

import ru.irenproject.TestCheck;
import ru.irenproject.inputQuestion.InputQuestionProtos.SpacePreprocessMode;
import ru.irenproject.store.Stash;

public final class RegexpPattern extends AbstractPattern {
  public static final int MAX_SIZE = 200;

  @Stash private InputQuestionProtos.RegexpPattern.Builder $;

  @Inject RegexpPattern() {}

  @Override protected void initialize() {
    super.initialize();
    setCaseSensitive(false);
    setSpacePreprocessMode(SpacePreprocessMode.NORMALIZE);
  }

  public boolean hasDefaultOptions() {
    return !caseSensitive() && (spacePreprocessMode() == SpacePreprocessMode.NORMALIZE);
  }

  @Override public String value() {
    return $.getValue();
  }

  @Override public void setValue(String value) {
    TestCheck.input(value.length() <= MAX_SIZE);
    $.setValue(value);
    post(ChangeEvent.INSTANCE);
  }

  @Override public int qualityPercent() {
    return $.getQualityPercent();
  }

  @Override protected void internalSetQualityPercent(int qualityPercent) {
    $.setQualityPercent(qualityPercent);
    post(ChangeEvent.INSTANCE);
  }

  public boolean caseSensitive() {
    return $.getCaseSensitive();
  }

  public void setCaseSensitive(boolean caseSensitive) {
    $.setCaseSensitive(caseSensitive);
    post(ChangeEvent.INSTANCE);
  }

  public SpacePreprocessMode spacePreprocessMode() {
    return $.getSpacePreprocessMode();
  }

  public void setSpacePreprocessMode(SpacePreprocessMode mode) {
    $.setSpacePreprocessMode(mode);
    post(ChangeEvent.INSTANCE);
  }

  @Override public void setOptionsFrom(Pattern source) {
    if (source instanceof RegexpPattern) {
      RegexpPattern p = (RegexpPattern) source;
      setCaseSensitive(p.caseSensitive());
      setSpacePreprocessMode(p.spacePreprocessMode());
    }
  }
}
